package com.yihuan.controller;

import com.yihuan.entity.OrderEntity;
import com.yihuan.entity.UserPlaintEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.global.vms_variable_global;
import com.yihuan.service.OrderService;
import com.yihuan.service.UserPlaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/5/6 14:11
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class UserPlaintController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    //全局访问类
    private vms_variable_global vms_variable = new vms_variable_global();
    @Autowired
    UserPlaintService userPlaintService;
    @Autowired
    OrderService orderService;
    
    @RequestMapping("/insertUserPlaintByUserPlaintData")
    public Map<String, Object> insertUserPlaintByUserPlaintData(@RequestBody UserPlaintEntity model, HttpSession session) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderID(model.getOrderID());
        List<OrderEntity> findOrderPriceByOrderPrice = orderService.findOrderPriceByOrderPrice(orderEntity);
        //用户编号
        model.setUserID(findOrderPriceByOrderPrice.get(0).getUserID());
        //回收员编号
        model.setUsersID(findOrderPriceByOrderPrice.get(0).getUsersID());
        //设置状态
        model.setUserPlaintState(1);
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setUserPlaintDate(sf.format(d));
        //登记用户数据
        int insertUserPlaintByUserPlaintData = userPlaintService.insertUserPlaintByUserPlaintData(model);
        if (insertUserPlaintByUserPlaintData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_PLAINT_INSERT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.USER_PLAINT_INSERT_SUCCESS);
        return map;
    }
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserPlaintByUserPlaintList")
    public Map<String, Object> findUserPlaintByUserPlaintList(@RequestBody UserPlaintEntity model, HttpSession session) {
        model.setUsersID((Integer) session.getAttribute("UserID_3"));
        List<UserPlaintEntity> findUserPlaintByUserPlaintList = userPlaintService.findUserPlaintByUserPlaintList(model);
        if (findUserPlaintByUserPlaintList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.PLAINT_LIMIT_FALL);
            return map;
        }
        pageNumber = countUserPlaintList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("userPlaint_List", findUserPlaintByUserPlaintList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.PLAINT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countUserPlaintList(UserPlaintEntity model) {
        count = userPlaintService.countUserPlaintList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserPlaintByUserPlaintList_1")
    public Map<String, Object> findUserPlaintByUserPlaintList_1(@RequestBody UserPlaintEntity model, HttpSession session) {
        List<UserPlaintEntity> findUserPlaintByUserPlaintList = userPlaintService.findUserPlaintByUserPlaintList(model);
        if (findUserPlaintByUserPlaintList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.PLAINT_LIMIT_FALL);
            return map;
        }
        pageNumber = countUserPlaintList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("userPlaint_List", findUserPlaintByUserPlaintList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.PLAINT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserPlaintByUserPlaint")
    public Map<String, Object> findUserPlaintByUserPlaint(@RequestBody UserPlaintEntity model, HttpSession session) {
        List<UserPlaintEntity> findUserPlaintByUserPlaint = userPlaintService.findUserPlaintByUserPlaint(model);
        if (findUserPlaintByUserPlaint.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.PLAINT_LIMIT_FALL);
            return map;
        }
        pageNumber = countUserPlaint(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("userPlaint_List", findUserPlaintByUserPlaint);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.PLAINT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countUserPlaint(UserPlaintEntity model) {
        count = userPlaintService.countUserPlaint(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
}
